import com.mochimedia.ui.*;

/**
* ...
* @author $(DefaultUser)
*/

class com.mochimedia.ui.HTMLField extends Component implements IComponent {
    
    private var _type:String = "htmlfield";
    
    private var _text:String;
    private var _tf:TextField;
    
    private var _multiLine:Boolean = true;
    
    private var _textOverflowing:Boolean = false;
    public function get textOverflowing():Boolean { return _textOverflowing; }
    
    private var _otf:TextField;
    private var _otfButton:MovieClip;
    
    public function get value ():String { return _text; }
    public function set value (val:String):Void { reset(val); }

	public var linkEvent:String = "";

    //
    //
    public function HTMLField (container:MovieClip, text:String, width:Number, multiLine:Boolean, position:Position, style:Style) {
        
        init (container, text, width, multiLine, position, style);

        if (_container != null) create();
        
    }
    
    //
    //
    private function init (container:MovieClip, text:String, width:Number, multiLine:Boolean, position:Position, style:Style):Void {
        
        super.init(container, position, style);
        
        _text = text;
        _width = width;
        
        _multiLine = (multiLine != undefined) ? multiLine : _multiLine;

    }
    
    //
    //
    public function create ():Void {

        super.create();
        
        if (_text == null || _text == "null" || _text == "undefined") return;
        
        _mc.createTextField("_text", 1, 0, 0, 500, 10);
        _tf = _mc["_text"];
        
        _tf.selectable = false;
        _tf.autoSize = "left";
        _tf.multiline = _multiLine;
        _tf.wordWrap = _multiLine;
        
        _tf.condenseWhite = true;
        _tf.embedFonts = _style.embedFonts;
        
        if (_style.embedFonts) {
            
            if (_text.indexOf("<h") == -1) {
                _tf.setNewTextFormat(new TextFormat(_style.font, _style.fontSize, _style.textColor, false));
            } else {
                _tf.setNewTextFormat(new TextFormat(_style.font, _style.titleFontSize, _style.titleColor, true));
            }
            
            _tf.html = false;
            _tf.text = stripTags(_text);

        } else {
            
            _tf.html = true;
            _tf.styleSheet = _style.styleSheet;
            _tf.htmlText = _text;
            
        }

        // NOTE: this makes no sense. switching off autosize causes the field to collapse. flash text sizing is just broken.
        
        if (!isNaN(_width) && _width > 0) {
            
            if (!_multiLine) _height = _tf._height; // record autosize height;
            
            var oldWidth:Number = _tf._width;
            
            if (!_multiLine) _tf.autoSize = false; // turn off autosize (collapse!)
            
            _tf._width = _width;
            
            if (!_multiLine) _tf._height = _height; // restore proper height
            if (!_multiLine && oldWidth > _width) _textOverflowing = true;

        } else if (isNaN(_width) && _parentCell != undefined) {
            
            _tf._width = _parentCell.width - _position.margin_right - _position.margin_left;
            
        }

        // set component size to textfield size
        _width = _tf._width;
        _height = _tf._height;
        
        if (textOverflowing) {

            _mc.createTextField("_otf", _mc.getNextHighestDepth(), _tf._x + _tf._width - 15, _tf._y, 15, _tf._height);
            _otf = _mc["_otf"];
            
            _otf.background = true;
            _otf.backgroundColor = _style.backgroundColor;
            
            if (_text.indexOf("<h") == -1) {
                _otf.setNewTextFormat(new TextFormat(_style.font, _style.fontSize, _style.textColor, true, false, false, "", "", "right"));
            } else {
                _otf.setNewTextFormat(new TextFormat(_style.font, _style.titleFontSize, _style.titleColor, true, false, false, "", "", "right"));
            }

            _otf.selectable = false;
            _otf.text = "...";
            
            if (_tf.htmlText.indexOf("</p") != -1 || _tf.htmlText.indexOf("</h") != -1) {
                _tf.htmlText = _tf.htmlText.split("</p").join("     .</p").split("</h").join("    .</h");
            } else {
                _tf.htmlText += "       .";
            }
            
            _otfButton = _mc.createEmptyMovieClip("scroller", _mc.getNextHighestDepth());
            DrawingMethods.rect(_otfButton, false, _tf.x, _tf._y, _tf._width, _tf._height, 0x000000, 0);
            _otfButton.useHandCursor = false;
            
            var field:TextField = _tf;
            _otfButton.onRollOver = function ():Void { this.onEnterFrame = function ():Void { field.hscroll+=2; } }
            _otfButton.onRollOut = function ():Void { field.hscroll = 0; delete this.onEnterFrame; }
            
            if (_text.indexOf("href='") != -1) {
                var link:String = _text.split("href='")[1].split("'")[0];
                _otfButton.onRelease = function ():Void { getURL(link, "_blank"); }
            }
            
        }
        
		var self:Object = this;
		_mc.dispatchlink = function (eventType:String):Void { self.dispatchLink(eventType); }
        
    }
	
	//
	//
	public function dispatchLink (en:String):Void {

		linkEvent = en;
		dispatchEvent(EVENT_CLICK);
		linkEvent = "";
		
    }
    
    //
    //
    private function reset (text:String):Void {
        
        _text = _tf.htmlText = text;
        
        //if (_tf != undefined) _tf.removeTextField();
        //if (_otf != undefined) _otf.removeTextField();
        //if (_otfButton != undefined) _otfButton.removeMovieClip();
        
        //create();
        
    }
    //
    //
    function stripTags (text:String):String {
        
        var i:Number;
        
        while ((i = text.indexOf("<")) != -1) text = text.split(text.substr(i, text.indexOf(">") - i + 1)).join("");
        
        return text;
        
    }
    
}